<%@page import="com.loyal.loyalerp.utils.ValidateUtil"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ include file="../../../../header.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>公司管理</title>

</head>
<body>
	<div class="mod-toolbar-top cf" style="margin-top: 10px;">
		<div class="fr" >
			<a href="javascript:void();" class="ui-btn ui-btn-sp mrb"
				id="btn-add">新增</a> 
			<a href="javascript:void();" class="ui-btn ui-btn-sp mrb"
		id="btn-search">搜索</a> 
			<a href="javascript:void(0);" class="ui-btn" id="btn-record">刷新</a> 
			
		
		</div>
	</div>
	<div class="grid-wrap" align="left" style="margin-left: 10px;">
		<table id="grid1"></table>
		<div id="pager2"></div>
	</div>
	<div class="grid-wrap" align="left" style="margin-left: 10px;">
		<table id="grid2"></table>
	</div>
	<div class="grid-wrap" align="left" style="margin-left: 10px;">
		<table id="grid3"></table>
	</div>
	<script type="text/javascript">
		
		//添加图标
		function operationFormat(cellValue, options, rowObject) {
			return '<div class="operating" data-id="' + options.rowId + 
			'"><span class="ui-icon ui-icon-pencil" title="编辑" data-toggle="modal" data-target="#myModal"></span>&nbsp&nbsp'
					+ '<span class="ui-icon ui-icon-trash" title="删除"></span>';
		}
		$(function() {
			$("#grid1").jqGrid(
					{
						url : '${pageContext.request.contextPath}/company/Companylist',//组件创建完成之后请求数据的url
						datatype : "json",
						height : 400,
						width : 1160,
						shrinkToFit : false,
						autoScroll : false,
						rownumbers : true,
						rowNum : -1,
						cellEdit : true,
						cellsubmit : "clientArray",
						colNames : [ '公司编号', '公司名称', '英文名称','父级公司','公司级别','末级标志','负责人', '电话','地址','传真','网址','状态','操作' ],
						colModel : [ //jqGrid每一列的配置信息。包括名字，索引，宽度,对齐方式.....
							
							{
								name : 'companycode',
								index : 'companycode',
								align : "center",
							//	width : 90,
							//	searchoptions:{sopt:['cn']},
							}, {
								name : 'companyname',
								index : 'companyname',
								width :195,
								sortable : false,
								align : "center",
								//editable : true,
								editoptions : {
									size : 10
								},
								
							},{
								name : 'companyengname',
								index : 'companyengname',
							//	width : 90,
								align : "center",
								hidden : true,
								sortable : false,
								editoptions : {
									size : 10
								},
							
							},{
								name : 'parentid',
								index : 'parentid',
							//	width : 100,
								align : "center",
								hidden : true,
								sortable : false,
								editoptions : {
									size : 10
								},
								search:false,
							}, {
								name : 'codelevel',
								index : 'codelevel',
							//	width : 100,
								align : "center",
								hidden : true,
								sortable : false,
								editoptions : {
									size : 10
								},
								search:false,
							},{
								name : 'endflag',
								index : 'endflag',
							//	width : 100,
								align : "center",
								sortable : false,
								hidden : true,
								editoptions : {
									size : 10
								},
								search:false,
							},{
								name : 'principal',
								index : 'principal',
							//	width : 100,
								align : "center",
								sortable : false,
							//	editable : true,
								editoptions : {
									size : 10
								},
								search:false,
							}, {
								name : 'telephone',
								index : 'telephone',
								sortable : false,
						//		width : 100,
								align : "center",
							//	editable : true,
								editoptions : {
									size : 10
								},
								search:false,
							},{
								name : 'address',
								index : 'address',
							//	width : 80,
							//	editable : true,
								align : "center",
								sortable : false,
							
								editoptions : {
									size : 10
								},
								search:false,
							},{
								name : 'fax',
								index : 'fax',
							//	width : 100,
								align : "center",
								hidden : true,
								sortable : false,
								editoptions : {
									size : 10
								},
								search:false,
							},{
								name : 'website',
								index : 'website',
								sortable : false,
							//	width : 100,
								align : "center",
								hidden : true,
								editoptions : {
									size : 10
								},
								search:false,
							},
							{
								name : 'active',
								index : 'active',
							//	width : 80,
							//	editable : true,
								align : "center",
								sortable : false,
								editoptions : {
									size : 10
								},
								search:false,
							},
							{
								
								name : "operating",
								sortable : false,
								//width : 195,
								formatter : operationFormat,
								align : "center",
								sortable : false
							}

							],	
						
						rowNum:10,//一页显示多少条
						pager : '#pager2',//表格页脚的占位符(一般是div)的id
						
						loadComplete : function() {
							loadover();
						},
						gridComplete : function() {
						},
						afterInsertRow : function() {
						},
						beforeEditCell : function(rowid, cellname, v, iRow,
								iCol) {
							lastrow = iRow;
							lastcell = iCol;
						},
						ondblClickRow:	function(rowid,iRow,iCol,e)
						{
						
							this.dialog = $.dialog({
								
							    title:'公司详情',
								content:"url:${pageContext.request.contextPath}/company/detaildialog?id="+rowid,
								width: 500,
								height: 250,
								max: false,
								min: false,
								cache: false,
								lock: true
							});
							
						}

					});
			initTableRow();
			initEvent();
		})
		//设置每行的高度
		function loadover() {
			var grid = $("#grid1");
			var ids = grid.getDataIDs();
			for (var i = 0; i < ids.length; i++) {
				grid.setRowData(ids[i], false, {
					height : 35
				});
			}
		}
		function initTableRow() {
			for (var i = 1; i <= 4; i++) {
				jQuery("#grid1").jqGrid('addRowData', i, {});
			}
			loadover();
		}
		//添加事件
		function initEvent() {
			$("#btn-add").click(function(a) {
				handle.operate("add");
			}) , $("#btn-search").click(function(a){
				handle.search();
			}), $("#grid1").on("click", ".operating .ui-icon-pencil", function(a) {
				var rowid = $(this).parent().data("id");
				var rowdata=$("#grid1").jqGrid("getRowData",rowid);
				handle.operate("edit",rowid,rowdata);
			}), $("#grid1").on("click", ".operating .ui-icon-trash", function(a) {
				a.preventDefault();
				var rowid = $(this).parent().data("id");
				handle.del(rowid);
			}),$("#btn-record").click(function(a) {
				$('#grid1').jqGrid('setGridParam',{search:false});  
				$("#grid1").jqGrid().trigger("reloadGrid");
			}),$("#btn-setsession").click(function(a){
				$.ajax({
					url:encodeURI("user/setsession"),
					type:"POST",
					success:function(data){
						alert(data);
					}
				});	
			}),$("#btn-getsession").click(function(a){
				$.ajax({
					url:encodeURI("user/getsession"),
					type:"POST",
					success:function(data){
						alert(data);
					}
				});	
			})
		}
		handle = {
				operate: function(oper, rowid, rowdata) {
					var title;
					var data={};
					if ("add" == oper) {
						title = "新增公司";
						data={
							operate: oper,
							callback: this.callback
						}
					} else {
						title = "修改信息";
						data={
							operate:oper,
							rowdata:rowdata,
							rowid:rowid,
							callback:this.callback
						}
					}
					this.dialog = $.dialog({
						title: title,
						data:data,
						content:"url:${pageContext.request.contextPath}/company/editdialog",
						width: 500,
						height: 250,
						max: false,
						min: false,
						cache: false,
						lock: true
					});
				},search:function(){
					var grid=$("#grid1");
					this.dialog = $.dialog({
						title : "搜索",
						data : {
							callback:this.callback,
							grid:grid
						},
						content : "url:${pageContext.request.contextPath}/company/searchDialog",
						width : 500,
						height : 100,
						max : false,
						min : false,
						cache : false,
						lock : true
					});
				},
				del: function(rowId) {
					$.dialog.confirm("删除数据将不能恢复，其公司关联的部门,员工，用户等信息都会被清空，请确认是否删除？", function() {
						
						$.ajax({
							url:encodeURI("${pageContext.request.contextPath}/company/delete"),
							type:"POST",
							data:{
								"id":rowId
							},
							success:function(data){
								if(data.status==200)
								alert('删除成功');
								else
								alert(data.message);
								$("#grid1").jqGrid().trigger("reloadGrid");	
							}
						});
					});
				},
				callback: function(data) {
				
					if(data!=undefined){
						$('#grid1').jqGrid('setGridParam',{search:true});  
						$("#grid1").jqGrid("setGridParam",{postData:data}).jqGrid("setGridParam",{page:1}).trigger("reloadGrid");
					}else{
						
						$("#grid1").jqGrid().trigger("reloadGrid");
					}
				}
			};
		
	</script>
	<script>
<%
if (!ValidateUtil.validateQuery("gy_company"))
{
%>
alert("不具备查看公司权限");
<%}%>
</script>
</body>
</html>